package com.qfedu.aunt.commons.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.aunt.commons.exception.BusinessException;
import com.qfedu.aunt.commons.mapper.*;
import com.qfedu.aunt.commons.pojo.*;
import com.qfedu.aunt.commons.result.ResponseCode;
import com.qfedu.aunt.commons.service.AuntService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.aunt.commons.vo.AuntVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 阿姨表 服务实现类
 * </p>
 *
 * @author xiaobobo
 * @since 2022-10-26
 */
@Service
public class AuntServiceImpl extends ServiceImpl<AuntMapper, Aunt> implements AuntService {

    @Autowired
    private AuntMapper auntMapper;

    @Autowired
    private WorkExperienceMapper workExperienceMapper;

    @Autowired
    private AuntMienMapper auntMienMapper;

    @Autowired
    private TrainingRecordsMapper trainingRecordsMapper;

    @Autowired
    private AgentMapper agentMapper;

    @Override
    public IPage<Aunt> findAuntsByCityNames(String cityName, Integer currentPage, Integer pageSize) throws Exception{
        if(null==currentPage || null==pageSize  ||
                StringUtils.isEmpty(cityName)){
            throw new BusinessException(ResponseCode.PARAMETER_ERROR.getCode(),
                    ResponseCode.PARAMETER_ERROR.getMessage());
        }
        //程序执行到这里说明参数是对的

        QueryWrapper<Aunt> auntQueryWrapper = new QueryWrapper<>();
        auntQueryWrapper.eq("city_name",cityName);

        Page<Aunt> page=new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageSize);

        //接下来查询

        IPage<Aunt> auntIPage = auntMapper.selectPage(page, auntQueryWrapper);
        return auntIPage;
    }

    @Override
    public AuntVo findAuntByAuntId(String auntId) {
        if(StringUtils.isEmpty(auntId)){
            throw new BusinessException(ResponseCode.PARAMETER_ERROR.getCode(),
                    ResponseCode.PARAMETER_ERROR.getMessage());
        }
        //接下来首先初始化我们的对象
        AuntVo auntVo = new AuntVo();
        //接下来查询阿姨的数据
        Aunt aunt = auntMapper.selectById(auntId);
        auntVo.setAunt(aunt);

        //接下来查询阿姨相关的工作履历
        QueryWrapper<WorkExperience> workExperienceQueryWrapper = new QueryWrapper<>();
        workExperienceQueryWrapper.eq("aunt_id",auntId);
        List<WorkExperience> workExperiences = workExperienceMapper.selectList(workExperienceQueryWrapper);
        auntVo.setExperiences(workExperiences);

        //接下来查询阿姨的风采数据
        QueryWrapper<AuntMien> auntMienQueryWrapper = new QueryWrapper<>();
        auntMienQueryWrapper.eq("aunt_id", auntId);
        List<AuntMien> auntMiens = auntMienMapper.selectList(auntMienQueryWrapper);
        auntVo.setAuntMiens(auntMiens);

        //接下来查询阿姨的培训记录信息
        List<TrainingRecords> trainingRecords=trainingRecordsMapper.findTrainingRecordsByAuntId(auntId);
        auntVo.setTrainingRecords(trainingRecords);

        //接下来查询这个阿姨的经纪人
        List<Agent> agents=agentMapper.findAgentsByAuntId(auntId);
        auntVo.setAgents(agents);

        return auntVo;
    }
}
